/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.util;

import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import soccerscope.model.ClientScene;
import soccerscope.model.GameEvent;
import soccerscope.model.SceneSet;
import soccerscope.util.analyze.BackPassAnalyzer;
import soccerscope.util.analyze.BallAverageAnalyzer;
import soccerscope.util.analyze.BallPossessionAnalyzer;
import soccerscope.util.analyze.CornerKickAnalyzer;
import soccerscope.util.analyze.DribbleAnalyzer;
import soccerscope.util.analyze.FreeKickAnalyzer;
import soccerscope.util.analyze.FreeKickFaultAnalyzer;
import soccerscope.util.analyze.GoalAnalyzer;
import soccerscope.util.analyze.GoalKickAnalyzer;
import soccerscope.util.analyze.GoalieCatchAnalyzer;
import soccerscope.util.analyze.KickInAnalyzer;
import soccerscope.util.analyze.LostBallAnalyzer;
import soccerscope.util.analyze.OffsideAnalyzer;
import soccerscope.util.analyze.PassAnalyzer;
import soccerscope.util.analyze.PassChainAnalyzer;
import soccerscope.util.analyze.PassDistAnalyzer;
import soccerscope.util.analyze.PassMissAnalyzer;
import soccerscope.util.analyze.PassVelAnalyzer;
import soccerscope.util.analyze.PassXAnalyzer;
import soccerscope.util.analyze.SceneAnalyzer;
import soccerscope.util.analyze.ShootAnalyzer;
import soccerscope.util.analyze.TopXAnalyzer;

public class GameAnalyzer {

	private static ArrayList<SceneAnalyzer> analyzerList;

	private final static String colName[] = { " ", "Left Team", "Right Team" };

	private GameAnalyzer() {
	};

	static {
		analyzerList = new ArrayList<SceneAnalyzer>();
		analyzerList.add(new GoalAnalyzer());
		analyzerList.add(new ShootAnalyzer());
		analyzerList.add(new DribbleAnalyzer());
		analyzerList.add(new GoalieCatchAnalyzer());
		analyzerList.add(new GoalKickAnalyzer());
		analyzerList.add(new OffsideAnalyzer());
		analyzerList.add(new BackPassAnalyzer());
		analyzerList.add(new CornerKickAnalyzer());
		analyzerList.add(new FreeKickAnalyzer());
		analyzerList.add(new FreeKickFaultAnalyzer());
		analyzerList.add(new KickInAnalyzer());
		analyzerList.add(new BallPossessionAnalyzer());
		analyzerList.add(new PassAnalyzer());
		analyzerList.add(new PassMissAnalyzer());
		analyzerList.add(new LostBallAnalyzer());
		analyzerList.add(new PassXAnalyzer());
		analyzerList.add(new TopXAnalyzer());
		analyzerList.add(new PassDistAnalyzer());
		analyzerList.add(new PassVelAnalyzer());
		analyzerList.add(new PassChainAnalyzer());
		analyzerList.add(new BallAverageAnalyzer());
	}

	private static void init() {
		Iterator it = analyzerList.iterator();
		while (it.hasNext()) {
			((SceneAnalyzer) it.next()).init();
		}
	}

	public static ArrayList<GameEvent> analyze(SceneSet set) {
		ArrayList<GameEvent> eventList = new ArrayList<GameEvent>();
		Iterator sceneit = set.iterator();
		ClientScene clientScene = null;
		ClientScene prev = null;
		GameEvent ge;

		init();
		while (sceneit.hasNext()) {
			prev = clientScene;
			clientScene = (ClientScene) sceneit.next();
			Iterator it = analyzerList.iterator();
			while (it.hasNext()) {
				ge = ((SceneAnalyzer) it.next()).analyze(clientScene, prev);
				if (ge != null && !eventList.contains(ge)) {
					eventList.add(ge);
				}
			}
		}
		return eventList;
	}

	public static SceneAnalyzer getAnalyzer(String name) {
		Iterator it = analyzerList.iterator();
		while (it.hasNext()) {
			SceneAnalyzer sa = (SceneAnalyzer) it.next();
			if (name.compareTo(sa.getName()) == 0)
				return sa;
		}
		return null;
	}

	public static TableModel getTableModel() {
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = -7927210501116649147L;

			@Override
			public String getColumnName(int col) {
				switch (col) {
				case SceneAnalyzer.ROW_NAME:
				case SceneAnalyzer.LEFT:
				case SceneAnalyzer.RIGHT:
					return colName[col];
				default:
					return " ";
				}
			}

			public int getColumnCount() {
				return SceneAnalyzer.COL_MAX;
			}

			public int getRowCount() {
				return analyzerList.size();
			}

			public Object getValueAt(int row, int col) {
				return ((SceneAnalyzer) analyzerList.get(row)).getValueAt(col);
			}
		};

		return dataModel;
	}

	public final static int timeSlice = 1000;

	public static TableModel getLeftTableModel() {
		TableModel dataModel = new AbstractTableModel() {

			private static final long serialVersionUID = -1688649502638591952L;

			@Override
			public String getColumnName(int col) {
				if (col == 0)
					return " ";
				return ((col - 1) * timeSlice + 1) + " - " + (col * timeSlice);
			}

			public int getColumnCount() {
				return Time.GAME_TIME / timeSlice + 1;
			}

			public int getRowCount() {
				return analyzerList.size();
			}

			public Object getValueAt(int row, int col) {
				if (col == 0)
					return ((SceneAnalyzer) analyzerList.get(row))
							.getValueAt(col);
				return ((SceneAnalyzer) analyzerList.get(row)).getLeftValueAt(
						((col - 1) * timeSlice + 1), (col * timeSlice));
			}
		};

		return dataModel;
	}

	public static TableModel getRightTableModel() {
		TableModel dataModel = new AbstractTableModel() {
			private static final long serialVersionUID = -7995033203508524075L;

			@Override
			public String getColumnName(int col) {
				if (col == 0)
					return " ";
				return ((col - 1) * timeSlice + 1) + " - " + (col * timeSlice);
			}

			public int getColumnCount() {
				return Time.GAME_TIME / timeSlice + 1;
			}

			public int getRowCount() {
				return analyzerList.size();
			}

			public Object getValueAt(int row, int col) {
				if (col == 0)
					return ((SceneAnalyzer) analyzerList.get(row))
							.getValueAt(col);
				return ((SceneAnalyzer) analyzerList.get(row)).getRightValueAt(
						((col - 1) * timeSlice + 1), (col * timeSlice));
			}
		};

		return dataModel;
	}
}
